package org.squabble.web.admin;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.Validator;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.squabble.SquabbleConstants;
import org.squabble.domain.Account;
import org.squabble.domain.Article;
import org.squabble.domain.Tag;
import org.squabble.service.AccountService;
import org.squabble.service.ArticleService;

@Controller
@RequestMapping("/admin/news/edit")
@SessionAttributes(value = { "account" })
public class NewsEditController {

	@Autowired
	ArticleService articleService;

	@Autowired
	AccountService accountService;

	@Autowired
	@Qualifier("validator")
	Validator validator;

	@RequestMapping(method = RequestMethod.GET)
	public String setupForm(
			@RequestParam(value = "id", required = false) Long articleId,
			@ModelAttribute("account") Account account, ModelMap model) {
		NewsEditCommand nec = new NewsEditCommand();
		if (articleId != null) {
			Article article = articleService.getArticle(articleId);
			if (article == null) {
				model.addAttribute(SquabbleConstants.POST_PROCESS_MESSAGE,
						"admin.news.edit.processed[0]");
				return "redirect:admin.news";
			}
			nec.setArticleId(article.getId());
			nec.setByline(article.getByline());
			nec.setAnnouncement(article.isAnnouncement());
			nec.setContent(article.getContent());
			nec.setDisplayDate(article.getDisplayDate());
			nec.setDisplayTime(article.getDisplayDate());
			nec.setReplyable(nec.isReplyable());
			nec.setTitle(article.getTitle());
			nec.setUser(article.getWriter().getName());
			nec.setDelete(article.isDeleted());
			StringBuffer tagbuffer = new StringBuffer();
			Set<Tag> tags = article.getTags();
			for (Tag tag : tags) {
				tagbuffer.append(tag.getName()).append((" "));
			}
			nec.setTags(tagbuffer.toString());
			model.addAttribute("writer", article.getWriter());
		} else {
			nec.setUser(account.getName());
			nec.setDisplayDate(new Date());
			nec.setDisplayTime(new Date());
			model.addAttribute("writer", account);
		}
		model.addAttribute("newsEditCommand", nec);

		return "admin.news.edit";
	}

	@RequestMapping(method = RequestMethod.POST)
	public String processSubmit(@ModelAttribute("account") Account account,
			@ModelAttribute("newsEditCommand") NewsEditCommand nec,
			BindingResult results, ModelMap model) {
		validator.validate(nec, results);
		if (results.hasErrors()) {
			Account writer = accountService.getAccount(nec.getUser());
			model.addAttribute("writer", writer);
			return "admin.news.edit";
		}

		articleService.saveArticle(nec, account);
		if (nec.isDelete()) {
			model.addAttribute(SquabbleConstants.POST_PROCESS_MESSAGE,
					"admin.news.edit.processed[2]");
		} else {
			model.addAttribute(SquabbleConstants.POST_PROCESS_MESSAGE,
					"admin.news.edit.processed[1]");

		}

		return "redirect:/admin/news";
	}

	@InitBinder
	public void initBinder(WebDataBinder binder) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		CustomDateEditor cde = new CustomDateEditor(sdf, true);
		binder.registerCustomEditor(Date.class, "displayDate", cde);
		sdf = new SimpleDateFormat("HH:mm");
		cde = new CustomDateEditor(sdf, true);
		binder.registerCustomEditor(Date.class, "displayTime", cde);
	}

}
